let app = getApp();
Page({
    data: {
        view:{
            height:100
        },
        userInfo: {},
        hasUserInfo: false,
        canIUse: wx.canIUse('button.open-type.getUserInfo')
    },
    onLoad: function (options) {
        this.setData({
            userInfo: app.globalData.userInfo
        })
    },
    onShow: function () {
        let that = this;
        if (app.globalData.userInfo != null){
            wx.request({
                url: app.globalData.serverUrl + 'user/getByUsername/' + app.globalData.userInfo.username,
                method: 'get',
                success:function (res) {
                    if (res.data.status == 200) {
                        that.setData({
                            hasUserInfo: true
                        })
                        that.setData({
                            userInfo: res.data.data
                        });
                        app.globalData.userInfo = res.data.data;
                    } else {
                        wx.showToast({
                            title: res.data.msg,
                            icon: 'none',
                            duration: 1500
                        });
                    }
                }
            })
            this.setData({
                hasUserInfo:true,
            })
        }
        this.tabBar();
      },
    tabBar(){
        if(typeof this.getTabBar === 'function' && this.getTabBar()){
            this.getTabBar().setData({
                selected:4
            });
        }
    },
    toMyCenter: function (){
        let that = this;
        if (this.data.hasUserInfo) {
            wx.navigateTo({
                url: '../my-center/my-center'
            });
        } else {
            that.toLogin();
        }

    },
    toLogin: function (){
        wx.navigateTo({
            url: '../login/login'
        });
    },
    toMyCircle: function () {
        let that = this;
        if (this.data.hasUserInfo) {
            wx.navigateTo({
                url: '../my-circle/my-circle'
            });
        } else {
            that.toLogin();
        }

    },
    changeHeadPic: function () {
        let that = this;
        wx.chooseImage({
            count: 1,
            sizeType: ['original', 'compressed'],
            sourceType: ['album', 'camera'],
            success (res) {
                // tempFilePath可以作为img标签的src属性显示图片
                const tempFilePaths = res.tempFilePaths
                console.log(tempFilePaths[0])
                wx.uploadFile({
                    url: app.globalData.serverUrl + 'qiniu', //仅为示例，非真实的接口地址
                    filePath: tempFilePaths[0],
                    name: 'file',
                    formData: {
                        'user': 'test'
                    },
                    success (res){
                        let data = JSON.parse(res.data)
                        //do something
                        if (data.status == 200){
                            that.setData({
                                userInfo: {
                                    id: app.globalData.userInfo.id,
                                    nickname: that.data.nickname,
                                    enrollmentYear: that.data.enrollmentYear,
                                    university: that.data.university,
                                    college: that.data.college,
                                    email: that.data.email,
                                    phone: that.data.phone,
                                    major: that.data.major,
                                    headPic: data.data.link
                                }
                            });
                            wx.request({
                                url: app.globalData.serverUrl + 'user/',
                                method: 'put',
                                data: that.data.userInfo,
                                headers:{
                                    'content-type':'application/json'
                                },
                                success: function (res) {
                                    if (res.data.status == 200) {
                                        wx.showToast({
                                            title: res.data.data.success,
                                            icon: 'none',
                                            duration: 1500
                                        });
                                        that.onLoad();
                                    }
                                }
                            })
                        }
                    }
                })
            }
        })
    }
});
